﻿using System;
using System.Reflection;
using System.Windows.Forms;
using System.Threading;

namespace DevOnServer
{
    public partial class CConsole : Form
    {
        public CConsole()
        {
            InitializeComponent();

            _setTitle();
            _setBuildVersion();

            s_instance = this;

            _startServer();
        }

        private void _setTitle()
        {
            Version version = Assembly.GetExecutingAssembly().GetName().Version;
            Text += string.Format(" v{0}.{1}", version.Major, version.Minor);
        }

        private void _setBuildVersion()
        {
            m_buildVersion.Text = "Build : " + Assembly.GetExecutingAssembly().GetName().Version.ToString();
        }

        private CDevOnServer m_server;

        private Thread m_serverThread;

        private void _startServer()
        {
            m_serverThread = new Thread(new ThreadStart(() =>
                {
                    while (!s_instance.IsHandleCreated)
                        Thread.Sleep(10);

                    m_server = new CDevOnServer();
                    m_server.start();
                }));
            m_serverThread.Start();
        }

        public delegate void DelegateTextOut(string msg);

        public void textOut(string msg)
        {
            m_textOut.AppendText(string.Concat(msg, Environment.NewLine));
        }

        private static CConsole s_instance;

        public static void WriteLine(string format, params object[] etc)
        {
            if (s_instance != null && s_instance.IsHandleCreated)
                s_instance.Invoke(new DelegateTextOut(s_instance.textOut), string.Format(format, etc));
        }

        protected override void OnFormClosed(FormClosedEventArgs e)
        {
            if (m_server != null
                && m_serverThread != null
                && m_serverThread.ThreadState == ThreadState.Running)
            {
                m_serverThread.Abort();
                m_server.exit();
            }
        }
    }

    public class CTextBox : TextBox
    {
        public CTextBox()
        {
            this.Multiline = true;
            this.ReadOnly = true;
        }

        private void checkForScrollbars()
        {
            bool scroll = false;
            int cnt = this.Lines.Length;
            if (cnt > 1)
            {
                int pos0 = this.GetPositionFromCharIndex(this.GetFirstCharIndexFromLine(0)).Y;

                if (pos0 >= 32768)
                    pos0 -= 65536;

                int pos1 = this.GetPositionFromCharIndex(this.GetFirstCharIndexFromLine(1)).Y;

                if (pos1 >= 32768)
                    pos1 -= 65536;

                int h = pos1 - pos0;
                scroll = cnt * h > (this.ClientSize.Height - 6);
            }
            if (scroll != mm_scrollbars)
            {
                mm_scrollbars = scroll;
                this.ScrollBars = scroll ? ScrollBars.Vertical : ScrollBars.None;
            }
        }

        protected override void OnTextChanged(EventArgs e)
        {
            checkForScrollbars();
            base.OnTextChanged(e);
        }

        protected override void OnClientSizeChanged(EventArgs e)
        {
            checkForScrollbars();
            base.OnClientSizeChanged(e);
        }

        private bool mm_scrollbars;
    }
}
